﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using NeuralStock.DomainService.Interfaces;
using NeuralStock.DomainService;
using Microsoft.Practices.Unity;
using System.Linq;
using System;

namespace NeuralStock.Tests.Data
{
    [TestClass]
    public class FinancialReportDataTest
    {
        [TestMethod]
        public void FinancialReport_KGHM2007_ShouldHaveValidValues()
        {
            Assert.AreEqual(string.Empty,
                ValidateFinancialReportFromMoney("KGHM", 2007, 12183113, 4682034, 3798826, 4468046, -577867, -3405550, 12423630, 3457681, 1439396, 2018285, 8965949, 200000));
        }

        [TestMethod]
        public void FinancialReport_ReportsBefore1997_ShouldNotExist()
        {
            IFinancialReportService financialReportService = Bootstrapper.GetUnityContainer().Resolve<IFinancialReportService>();

            var numberOfReports = financialReportService.GetAll().Where(x => x.Year < 1997).Count();

            Assert.AreEqual(0, numberOfReports);
        }

        [TestMethod]
        public void FinancialReport_ReportsAfter2012_ShouldNotExist()
        {
            IFinancialReportService financialReportService = Bootstrapper.GetUnityContainer().Resolve<IFinancialReportService>();

            var numberOfReports = financialReportService.GetAll().Where(x => x.Year > 2012).Count();

            Assert.AreEqual(0, numberOfReports);
        }

        [TestMethod]
        public void FinancialReport_ReportsForBanks_ShouldNotExist()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            IFinancialReportService financialReportService = Bootstrapper.GetUnityContainer().Resolve<IFinancialReportService>();

            var banks = companyService.GetAll().Where(x => "ban".Equals(x.Sector, StringComparison.InvariantCultureIgnoreCase));

            foreach (var bank in banks)
            {
                var numberOfReports = financialReportService.GetAllForCompany(bank.Id).Count();
                Assert.AreEqual(0, numberOfReports);
            }        
        }

        public string ValidateFinancialReportFromMoney(string companyName, int year,
            decimal? netRevenuesFromSalesOfGoodsAndMaterials, decimal? profitFromOperatingActivities, decimal? netProfit,
            decimal? netCashFlowsFromOperatingActivities, decimal? netCashFlowsFromInvestmentActivities, decimal? netCashFlowsFromFinancialActivities,
            decimal? assets, decimal? liabilitiesAndProvisionsForLiabilities, decimal? longTermLiabilities, decimal? shortTermLiabilities,
            decimal? equityCapital, decimal? numberOfShares)
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            IFinancialReportService financialReportService = Bootstrapper.GetUnityContainer().Resolve<IFinancialReportService>();

            var company = companyService.Get(companyName);
            var report = financialReportService.Get(company.Id, year);

            if (report.NetRevenuesFromSalesOfGoodsAndMaterials != netRevenuesFromSalesOfGoodsAndMaterials)
                return "NetRevenuesFromSalesOfGoodsAndMaterials different";
            if (report.ProfitFromOperatingActivities != profitFromOperatingActivities)
                return "ProfitFromOperatingActivities different";
            if (report.NetProfit != netProfit)
                return "NetProfit different";
            if (report.NetCashFlowsFromOperatingActivities != netCashFlowsFromOperatingActivities)
                return "NetCashFlowsFromOperatingActivities different";
            if (report.NetCashFlowsFromInvestmentActivities != netCashFlowsFromInvestmentActivities)
                return "NetCashFlowsFromInvestmentActivities different";
            if (report.NetCashFlowsFromFinancialActivities != netCashFlowsFromFinancialActivities)
                return "NetCashFlowsFromFinancialActivities different";
            if (report.Assets != assets)
                return "Assets different";
            if (report.LiabilitiesAndProvisionsForLiabilities != liabilitiesAndProvisionsForLiabilities)
                return "LiabilitiesAndProvisionsForLiabilities different";
            if (report.LongTermLiabilities != longTermLiabilities)
                return "LongTermLiabilities different";
            if (report.ShortTermLiabilities != shortTermLiabilities)
                return "ShortTermLiabilities different";
            if (report.EquityCapital != equityCapital)
                return "EquityCapital different";
            if (report.NumberOfShares != numberOfShares)
                return "NumberOfShares different";

            return string.Empty;
        }
    }
}
